<template>
  <!--项目类型-->
  <el-table :summary-method="getSummaries" show-summary :data="tableList" :span-method="objectSpanMethod" border class="statistics-list">
    <el-table-column prop="proStatus" align="center" label="项目状态">
      <template slot-scope="{row}">
        <span class="to-link" @click="goProject(row,'1')">{{ statusFormat(row) }}</span>
      </template>
    </el-table-column>
    <el-table-column prop="proTypeName" align="center" label="项目类型">
      <template slot-scope="{row}">
        <span class="to-link" @click="goProject(row,'2')">{{ row.proTypeName }}</span>
      </template>
    </el-table-column>
    <!--<el-table-column prop="landFormTotal" align="center" label="合计(个)" />
    <el-table-column prop="regionName" align="center" label="地区类型">
      <template slot-scope="{row}">
        <span class="to-link" @click="goProject(row,'3')">{{ row.regionName }}</span>
      </template>
    </el-table-column>
    <el-table-column prop="proTypeTotal" align="center" label="合计(个)" />
    <el-table-column prop="proLevel" align="center" label="项目等级">
      <template slot-scope="{row}">
        <span class="to-link" @click="goProject(row,'4')">{{ gradeFormat(row) }}</span>
      </template>
    </el-table-column>-->
    <el-table-column prop="proNumber" align="center" label="项目数(个)" />
    <el-table-column prop="contractAmount" :formatter="(row) => contractAmount(row)" align="center" label="项目产值(万元)" />
  </el-table>
</template>

<script>
import { mapState } from 'vuex'
import { selectProInfoByProTypeExport } from '@/api/statisticalAnalysis/statistics/project'
import { dataMethod } from '@/utils/tableObjectSpanMethod.js'
export default {
  name: 'ProjectType',
  props: {
    tableList: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      spanObj: {},
      regionList: []
    }
  },
  computed: {
    ...mapState({
      ProStatusList: (state) => state.basicInfo.ProStatusList,
      proGrade: state => state.basicInfo.proGrade
    }),
    statusFormat() {
      return row => (this.selectDictLabel(this.ProStatusList, row.proStatus))
    },
    gradeFormat() {
      return row => this.selectDictLabel(this.proGrade, row.proLevel)
    },
    contractAmount() {
      return row => this.divideMoney(row.contractAmount, 6, true)
    }
  },
  watch: {
    tableList: {
      immediate: true,
      handler(value) {
        if (value && value.length) {
          this.spanObj = dataMethod(this.tableList, ['proStatus', 'proTypeName', 'landFormTotal', 'regionName', 'proTypeTotal'])
          const regionList = this.tableList.map(item => ({ regionName: item.regionName, region: item.region }))
          this.regionList = this.deteleObject(regionList)
        }
      }
    }
  },
  mounted() {
  },
  methods: {
    // 数组里的对象数据去重
    deteleObject(arr) {
      const temp = {}	//	用于name判断
      const result = [] // 最后的新数组
      arr.map((item, index) => {
        if (!temp[item.region]) {
          result.push(item)
          temp[item.region] = true
        }
      })
      return result
    },
    // 表格合计
    getSummaries(param) {
      const { data } = param
      const proNumber = data.reduce((pre, row) => pre + row.proNumber * 1, 0)
      const contractAmount = data.reduce((pre, row) => pre + row.contractAmount * 1, 0)
      const sums = []
      sums.push('总计', '', proNumber, this.divideMoney(contractAmount, 6, true))
      return sums
    },
    objectSpanMethod({ row, column, rowIndex, columnIndex }) {
      // 列合并
      const _row = this.spanObj[column.property] ? this.spanObj[column.property][rowIndex] : 1
      const _col = _row > 0 ? 1 : 0
      return {
        rowspan: _row,
        colspan: _col
      }
    },
    goProject(row, flag) {
      const { proStatus: status, region, proTypeId, proLevel } = row
      let query = {}
      if (flag === '1') {
        query = { status }
      } else if (flag === '2') {
        query = { status, proTypeId }
      } else if (flag === '3') {
        query = { status, region, proTypeId }
      } else if (flag === '4') {
        query = { status, region, proTypeId, proLevel }
      }
      this.$router.push({ name: 'statisticsList-project', params: { ...query, cityOptions: this.regionList, currentTable: '2' }})
    },
    // 导出
    async exportData() {
      const tableHeader = [
        {
          'key': 'proStatus',
          'label': '项目状态'
        },
        {
          'key': 'proTypeName',
          'label': '项目类型'
        },
        /* {
          'key': 'landFormTotal',
          'label': '合计(个)'
        },
        {
          'key': 'regionName',
          'label': '地区类型'
        },
        {
          'key': 'proTypeTotal',
          'label': '合计(个)'
        },
        {
          'key': 'proLevel',
          'label': '项目等级'
        },*/
        {
          'key': 'proNumber',
          'label': '项目个数'
        },
        {
          'key': 'contractAmount',
          'label': '项目产值(万元)'
        }
      ]
      const res = await selectProInfoByProTypeExport(tableHeader)
      if (!res) return
      this.downloadFileFlow(res, '项目类型统计信息')
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
